module.exports = {
    sock: null,
    onOpen: function () {
        cc.log("连接成功");
    },
    
    onMessage: function (event) {
        //cc.log("接收消息:" + event.data);
    },
    
    onClose: function () {
        cc.log("关闭连接");
        this.close();
    },
    
    onError: function () {
        cc.log("发生错误");
        this.close();
    },
    
    close: function () {
        if(this.sock){
            this.sock.close();
            this.sock = null;
        }
    },
    
    connect: function (url,parent) {
        this.sock = new WebSocket(url);
        this.sock.onopen = this.onOpen;
        this.sock.onmessage = this.onMessage;
        this.sock.onclose = this.onClose;
        this.sock.onerror = this.onError;
        this.sock.parent = parent;
        return this;
    },
    
    sendData: function (data) {
        var json = JSON.stringify(data);
        cc.log("发送消息:"+json);

        this.sock.send(json);
    }
}